package com.platform.modules.act.jglx;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.annotation.UserIds;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.MapUtil;
import com.platform.common.utils.StringUtils;
import com.platform.modules.act.common.service.AssignService;
import com.platform.modules.act.ctx.service.ProcessObjectService;
import com.platform.modules.act.util.PoUtil;
import com.platform.modules.cpt.entity.CptAcceptanceEntity;
import com.platform.modules.cpt.service.CptAcceptanceService;
import com.platform.modules.prj.entity.PrjProjectEntity;
import com.platform.modules.prj.service.PrjProjectService;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysUserService;


/**
 * 竣工验收 人员分配服务
 * @author Administrator
 *
 */
@Service
public class JgysAssignService {
    private static final Logger logger = LoggerFactory.getLogger(JgysAssignService.class);
	
    @Autowired
    ProcessObjectService processObjectService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysOrgService sysOrgService;
    @Autowired
    AssignService assignService;
    @Autowired
    PrjProjectService projectService;
    @Autowired
    CptAcceptanceService cptAcceptanceService;
    
    
    List<String> list = Arrays.asList("52ba57053ee87f0004b93d187d42c979");
    

    /**
     * 建设 - 获取流程发起人
     */
    @UserIds
    public List<String> jgysjdsq(String poId, String userIds) {
    	if (StringUtils.isNotBlank(userIds)) {
            return Arrays.asList(userIds.split(","));
		}
    	return list;
    }
    
    /**
     * 监管 - 竣工验收监督条件核查
     */
    @UserIds
    public List<String> jgysjdtjhc(String poId, String userIds) {
    	String businessId = PoUtil.getBusinessId(poId);
    	SysOrgEntity org = queryCountyOrg(businessId);
    	return assignService.assignByOrgAndPermis(org.getOrgNo(), "jgysjdtjhc");
    }
    
    /**
     * 监管 - 竣工验收监督
     */
    @UserIds
    public List<String> jgysjd(String poId, String userIds) {
    	String businessId = PoUtil.getBusinessId(poId);
    	SysOrgEntity org = queryCountyOrg(businessId);
    	return assignService.assignByOrgAndPermis(org.getOrgNo(), "jgysjd");
    }
    
    /**
     * 建设 - 编写竣工验收情况
     */
    @UserIds
    public List<String> bxjgysqk(String poId, String userIds) {
    	String businessId = PoUtil.getBusinessId(poId);
    	SysOrgEntity org = queryBuildOrg(businessId);
    	return assignService.assignByOrgAndPermis(org.getOrgNo(), "bxjgysqk");
    }
    
    /**
     * 质监站 - 编写竣工验收监督报告
     */
    @UserIds
    public List<String> bxjgysjdbg(String poId, String userIds) {
    	String businessId = PoUtil.getBusinessId(poId);
    	SysOrgEntity org = queryCountyOrg(businessId);
    	return assignService.assignByOrgAndPermis(org.getOrgNo(), "bxjgysjdbg");
    }
    
    /**
     * 监管 - 盖章
     */
    @UserIds
    public List<String> jggz(String poId, String userIds) {
    	String businessId = PoUtil.getBusinessId(poId);
    	SysOrgEntity org = queryCountyOrg(businessId);
    	return assignService.assignByOrgAndPermis(org.getOrgNo(), "jggz");
    }
    
    /**
     * 建设-组织编写竣工验收报告
     */
    @UserIds
    public List<String> bxjgysbg(String poId, String userIds) {
    	String businessId = PoUtil.getBusinessId(poId);
    	SysOrgEntity org = queryBuildOrg(businessId);
    	return assignService.assignByOrgAndPermis(org.getOrgNo(), "bxjgysbg");
    }
    
    
//    /**
//     * 建设 - 组织竣工验收
//     */
//    @UserIds
//    public List<String> zzjgys(String poId, String userIds) {
//    	String businessId = PoUtil.getBusinessId(poId);
//    	PrjProjectEntity project = projectService.getById(businessId);
//    	return assignService.assignByOrgAndPermis(project.getBuildCorpId(), "zzjgys");
//    }
    
    /**
     * 获取项目 区县监管机构
     */
    private SysOrgEntity queryCountyOrg(String businessId) {
    	CptAcceptanceEntity cptAcceptanceEntity = cptAcceptanceService.getById(businessId);
    	if(null==cptAcceptanceEntity) {
    		logger.error("未找到竣工验收申请记录,id:{}",businessId);
    		throw new BusinessException("未找到竣工验收申请记录");
    	}
    	PrjProjectEntity project = projectService.getById(cptAcceptanceEntity.getPrjId());
    	String conutyNum = project.getCountyNum();
    	SysOrgEntity org = sysOrgService.getOne(new QueryWrapper<SysOrgEntity>().allEq(MapUtil.of("ORG_LEVEL","4","COUNTY_NO",conutyNum)));
    	return org;
    }
    
    /**
     * 获取竣工验收监督申请 建设单位
     */
    private SysOrgEntity queryBuildOrg(String businessId) {
    	CptAcceptanceEntity cptAcceptanceEntity = cptAcceptanceService.getById(businessId);
    	if(null==cptAcceptanceEntity) {
    		logger.error("未找到竣工验收申请记录,id:{}",businessId);
    		throw new BusinessException("未找到竣工验收申请记录");
    	}
    	SysOrgEntity org = sysOrgService.getOne(new QueryWrapper<SysOrgEntity>().allEq(MapUtil.of("ORG_NO",cptAcceptanceEntity.getBuildCorpId())));
    	return org;
    }
    
    
    
   
}
